package com.base.mail.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.base.mail.R;
import com.base.mail.bean.ContactEntity;
import com.base.mail.utils.LetterIndexer;

import java.util.List;

/**
 * Created by arvin on 2018/8/19.
 */

public class ContactAdapter extends BaseAdapter<ContactEntity> {
    public LetterIndexer mLetterIndexer;

    public ContactAdapter(List data, Context context) {
        super(data, context);
        mLetterIndexer = new LetterIndexer(mData);


    }


    @Override
    public void convert(BaseHolder holder, ContactEntity item, boolean isHeader, boolean isBottom) {
        int position = holder.getAdapterPosition();
        TextView tv_name = holder.getView(R.id.tv_name);
        TextView tv_head = holder.getView(R.id.tv_head);
        ImageView iv_head = holder.getView(R.id.iv_head);
        TextView letter_index = holder.getView(R.id.letter_index);
        RelativeLayout content = holder.getView(R.id.content);
        ToggleButton toggleButton = holder.getView(R.id.tb);
        View line = holder.getView(R.id.line);
        RelativeLayout rl = holder.getView(R.id.rl);
        ImageView iv_un = holder.getView(R.id.iv_un);


        tv_name.setText(item.getRealname());
        String name = item.getRealname();
        if (!TextUtils.isEmpty(name)) {
            String head = name.substring(name.length() - 1, name.length());
            tv_head.setText(head);
        }

        int section = mLetterIndexer.getSectionForPosition(position);
        if (mLetterIndexer.getPositionForSection(section) == position) {
            String letter = (String) mLetterIndexer.getSections()[section];
            letter_index.setText(letter);
            letter_index.setVisibility(View.VISIBLE);
        } else {
            letter_index.setVisibility(View.GONE);
        }
        line.setVisibility(View.VISIBLE);
        for (int i = 0; i < mLetterIndexer.getPositions().size(); i++) {
            if (position == (mLetterIndexer.getPositions().get(i) - 1)) {
                line.setVisibility(View.INVISIBLE);
                break;
            }
        }
        if (position == mData.size() - 1) {
            line.setVisibility(View.INVISIBLE);
        }
    }


    public void setData(List<ContactEntity> data)
    {
        mData.clear();
        mData.addAll(data);
        mLetterIndexer=new LetterIndexer(mData);
        notifyDataSetChanged();
    }
}
